import { ObjTypes } from "../appTypes";
import log from "./log"
var CryptoJS = require("crypto-js");

//创建token
export const creatToken = (id:number|string,account:any)=>{
  let payload = {
    account:account,
    id: id
  }
  var ciphertext = CryptoJS.AES.encrypt(JSON.stringify(payload), 'key').toString();
  return ciphertext;
}
//解析token
export const resolveToken = (token:string):Promise<ObjTypes | string>=>{
  return new Promise((reslove:any,reject:any)=>{
    if(!token){
      log.info("token不存在:"+token);
      reslove("token不存在")
      return
    }
    // Decrypt
    var bytes  = CryptoJS.AES.decrypt(token, 'key');
    var originalText = bytes.toString(CryptoJS.enc.Utf8);
    if(originalText){
      reslove(JSON.parse(originalText));
    }else{
      reslove('token错误')
    }
  })
  
}